package com.javasm.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();


        while (true){
            System.out.println("欢迎来到学生管理系统------------------------");
            System.out.println("1.添加学生------------------");
            System.out.println("2.删除学生------------------");
            System.out.println("3.修改学生------------------");
            System.out.println("4.查询学生------------------");
            System.out.println("5.退出------------------");
            System.out.println("请输入您的选择--------------------------------");

            String choice = input.next();
            switch (choice){
                case "1" :
                    addStudent(list);
                    break;
                case "2" :
                    deleteStudent(list);
                    break;
                case ("3"):
                    updateStudent(list);
                    break;
                case ("4") :
                    queryStudent(list);
                    break;
                case "5" :
                    System.out.println("正在退出了");
                    System.exit(0);

                default:
                    System.out.println("请输入对应的数字来选择你需要的操作");

            }




        }


    }

    private static void queryStudent(ArrayList<Student> list) {
        if(list.size() == 0){
            System.out.println("当前集合没有相关数据，请稍后再试");
            return;
        }
        System.out.println("id  \t  姓名\t 年龄 \t 家庭住址’");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getAddress());

        }
    }

    private static void updateStudent(ArrayList<Student> list) {


    }

    private static void deleteStudent(ArrayList<Student> list) {
        Scanner  sc = new Scanner(System.in);
        System.out.println("请输入要删除的id");
        String id = sc.next();
        int index =getIndex(list,id);
        if(index>0){
            list.remove(index);
            System.out.println("id为"+id+"的学生删除成功");
        }else{
            System.out.println("id不存在，删除失败！！！！！！！！！！！！！！");
        }


    }

    private static void addStudent(ArrayList<Student> list) {
        //创建一个键盘对象
        Scanner sc = new Scanner(System.in);
        Student student = new Student();
        while(true){
            System.out.println("请输入学生的ID");
            String id = sc.next();
            int index = getIndex(list,id);
            if(index==-1){
                student.setId(id);
                break;
            }else {
                System.out.println("id已经存在，请重新输入");
            }
        }
        nextInt(list, sc, student);
        list.add(student);
        System.out.println("这个学生的信息添加成功-----------");

    }

    private static void nextInt(ArrayList<Student> list, Scanner sc, Student student) {
        System.out.println("请输入学生的姓名");
        String name = sc.next();
        student.setName(name);
        System.out.println("请输入学生的年龄");
        int age = sc.nextInt();
        student.setAge(age);
        System.out.println("请输入家庭的住址");
        String address = sc.next();
        student.setAddress(address);

    }


    public static int getIndex (ArrayList<Student>list,String id){
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            String sid = student.getId();
            if(sid.equals(id)){
                return i;
            }
        }
        return -1;

    }

}
